package com.yunxin.core.application.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.utils.JacksonUtils;
import com.yunxin.core.application.assembler.PageAssembler;
import com.yunxin.core.application.assembler.PageDataAssembler;
import com.yunxin.core.application.command.page.GetPageConfigInfoCommand;
import com.yunxin.core.application.command.page.PagePageCommand;
import com.yunxin.core.application.dto.page.*;
import com.yunxin.core.application.service.PageQueryAdminService;
import com.yunxin.core.domain.external.product.ProductExternalService;
import com.yunxin.core.infra.persistence.entity.page.PageDO;
import com.yunxin.core.infra.persistence.entity.page.query.condition.PagePageCondition;
import com.yunxin.core.infra.persistence.entity.page.query.result.PageListResult;
import com.yunxin.core.infra.persistence.mapper.page.PageCacheExtendMapper;
import com.yunxin.core.infra.persistence.mapper.page.PageExtendMapper;
import com.yunxin.core.utils.infra.utils.PageQueryUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author zero°
 * @since 2023-05-27
 */
@Service
@RequiredArgsConstructor
public class PageQueryAdminServiceImpl implements PageQueryAdminService {

    private final PageCacheExtendMapper pageCacheExtendMapper;

    private final PageExtendMapper pageExtendMapper;

    private final ProductExternalService productExternalService;

    private final PageAssembler pageAssembler;

    @Override
    public List<PageListDTO> listPage() {
        List<PageListResult> results = pageExtendMapper.listPage();
        return results.stream().map(pageAssembler::toPageListDTO).toList();
    }

    @Override
    public PageResult<PagePageDTO> pagePageQuery(PagePageCommand command) {
        PagePageCondition condition = pageAssembler.fromPagePageCommand(command);
        long total = pageExtendMapper.countPagePageQuery(condition);
        if (total == 0) {
            return PageResult.success(Collections.emptyList(), total);
        }
        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
        List<PageDO> results = pageExtendMapper.pagePageQuery(condition);
        List<PagePageDTO> dtoList = results.stream().map(pageAssembler::toPagePageDTO).toList();
        return PageResult.success(dtoList, total);
    }

    @Override
    public PageConfigInfoDTO<PageComponentElementInfoDataAdminDTO> getPageConfig(GetPageConfigInfoCommand command) {
        String pageData = pageCacheExtendMapper.getLastPageCache(command.getPageCode(), null);
        PageInfoDataDTO pageConfig = JacksonUtils.X.parse(pageData, new TypeReference<PageInfoDataDTO<PageComponentElementInfoDataAdminDTO>>() {});
        PageConfigInfoDTO<PageComponentElementInfoDataAdminDTO> dto = new PageConfigInfoDTO<>();
        dto.setPageCode(command.getPageCode());
        PageDataAssembler.X.dealPageConfigInfoDTO(pageConfig, productExternalService);
        dto.setPageConfig(pageConfig);
        return dto;
    }


}
